package cn.fhkj.web.controller;

import cn.fhkj.bean.ResultBean;
import cn.fhkj.entity.RenQiuZfPublicity;
import cn.fhkj.entity.RenQiuZfUnit;
import cn.fhkj.service.RenqiuZfService;
import cn.fhkj.vo.ReconsiderationVo;
import cn.fhkj.vo.RenQiuZfPublicityTypeLv2Vo;
import cn.fhkj.vo.RenQiuZfPublicityVo;
import cn.fhkj.vo.RenQiuZfUnitVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: renqiu-publicity-web
 * @description: 执法单位控制器
 * @author: seven
 * @create: 2020-08-05 16:02
 **/
@RequestMapping("/rqzf")
@RestController
public class RenqiuZfController extends BaseController{
    private static final Logger logger = LoggerFactory.getLogger(RenqiuZfController.class);

    @Resource
    private RenqiuZfService renqiuZfService;
    /**
     * 查询所有单位
     * rul:/rqzf/getUnit
     */
    @RequestMapping("/getUnit")
    @ResponseBody
    public ResultBean getUnit(){
        ResultBean resultBean = new ResultBean();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<RenQiuZfUnitVo> voList = new ArrayList<>();
        RenQiuZfUnitVo vo;
        try{
        List<RenQiuZfUnit>  list = renqiuZfService.getUnit();
        for (RenQiuZfUnit r:list) {
            vo = new RenQiuZfUnitVo();
            vo.setId((r.getId().toString()));
            if (r.getCreateTime() != null){
                Date format = sdf.parse(r.getCreateTime());
                vo.setCreateTime(sdf.format(format));
            }
            vo.setName(r.getName());
            vo.setStatus(r.getStatus());
            vo.setUserName(r.getUserName());
            vo.setUserId((r.getUserId()).toString());
            vo.setPos(r.getPos());
            vo.setIcon(handleResToUrl(r.getIcon()));
            voList.add(vo);
        }
        resultBean.setData(voList);
        resultBean.setMsg("查询成功！");
        resultBean.setSuccess(ResultBean.SUCCESS);
        }catch (Exception e){
            logger.error("",e);
            resultBean.setMsg("查询失败！稍后重试");
            resultBean.setSuccess(ResultBean.FAIL);
        }
        return resultBean;
    }


    /**
     * 获取所有的执法单位 一级列表
     * url: /rqzf/getRenQiuLv1
     * */
    @RequestMapping("/getRenQiuLv1")
    @ResponseBody
    public ResultBean getRenQiuLv1(){
        return renqiuZfService.getRenQiuLv1();
    }

    /**
     * 动态根据一级列表和详情字 获取二级列表
     * url:/rqzf/getRenQiuLv2{Lv1Id}{Lv2Name}
     */
    @RequestMapping("/getRenQiuLv2")
    @ResponseBody
    public ResultBean getRenQiuLv2ByLv1(Integer index,Integer pageSize,Integer Lv1Id ,Long unitId) {
        ResultBean resultBean = new ResultBean();
        try {
            List<RenQiuZfPublicityTypeLv2Vo> lv2List = renqiuZfService.getRenQiuLv2ByLv1(index, pageSize, Lv1Id,unitId);
            countPage(resultBean,lv2List);
            resultBean.setMsg("查询成功！");
            resultBean.setSuccess(ResultBean.SUCCESS);
            resultBean.setData(lv2List);

        } catch (Exception e) {
            logger.error("",e);
            resultBean.setMsg("查询失败！稍后重试");
            resultBean.setSuccess(ResultBean.FAIL);
        }
        return resultBean;
    }

    /**
     * 富文本
     */
    @RequestMapping("/content")
    @ResponseBody
    public ResultBean getRenQiuLv2ByLv1(Long publicityId) {

        return renqiuZfService.getPublicityContentByPublicityId(publicityId);
    }

        /**
         * 查询详情信息
         * url:/rqzf/Publicity
         */
    @RequestMapping("/publicity")
    @ResponseBody
    public ResultBean getPublicity(Integer index,Integer pageSize,Long Lv2Id,Long Lv1Id){
        ResultBean resultBean = new ResultBean();
        try {
            List<RenQiuZfPublicityVo> list =  renqiuZfService.getPublicityByL2Id(index,pageSize,Lv2Id,Lv1Id);
            countPage(resultBean,list);
            resultBean.setMsg("查询成功！");
            resultBean.setSuccess(ResultBean.SUCCESS);
            resultBean.setData(list);
            Map<String,Object> map = new ConcurrentHashMap<>();
        } catch (Exception e) {
            logger.error("",e);
            resultBean.setMsg("查询失败！稍后重试");
            resultBean.setSuccess(ResultBean.FAIL);
        }
        return resultBean;
    }


    /**
     * 查询置顶文章
     * url:rqzf/topping
     */
        @RequestMapping("/topping")
    @ResponseBody
    public ResultBean getTopping(){
        return renqiuZfService.getTopping();
    }
    /**
     * 搜索框
     * url:rqzf/search
     */
    @RequestMapping("/search")
    @ResponseBody
    public ResultBean search(String name,Integer index,Integer pageSize,Long Lv1Id,Long Lv2Id,Long unitId){
        ResultBean resultBean = new ResultBean();
        try {
            List<RenQiuZfPublicity> list =  renqiuZfService.search(name,index,pageSize,Lv1Id,Lv2Id,unitId);
            countPage(resultBean,list);
            resultBean.setMsg("查询成功！");
            List<RenQiuZfPublicityVo> listVo = renqiuZfService.dataProcessing(list);
            resultBean.setSuccess(ResultBean.SUCCESS);
            resultBean.setData(listVo);
        } catch (Exception e) {
            logger.error("",e);
            resultBean.setMsg("查询失败！稍后重试");
            resultBean.setSuccess(ResultBean.FAIL);
        }
        return resultBean;
    }


    /**
     * 添加行政复议
     * url:rqzf/add/rec
     */
    @PostMapping("/add/rec")
    public ResultBean addReconsideration(ReconsiderationVo vo){
        return renqiuZfService.addReconsideration(vo);
    }


    @PostMapping("/list")
    public String list(Model model, HttpServletRequest request){
        String level1 = request.getParameter("level1");
        String level2 = request.getParameter("level2");
        String names = request.getParameter("names");
        String typeId = request.getParameter("typeId");
        String name = request.getParameter("name");
        String formType = request.getParameter("formType");
        model.addAttribute("level1",level1);
        model.addAttribute("level2",level2);
        model.addAttribute("names",names);
        model.addAttribute("typeId",typeId);
        model.addAttribute("name",name);
        model.addAttribute("formType",formType);
        return "/list";
    }

}
